package com.ssy.lingxi.product.controller.pc.material;

import com.ssy.lingxi.common.constant.tree.Node;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.product.entity.material.MaterialGroup;
import com.ssy.lingxi.product.model.request.CommonIdVO;
import com.ssy.lingxi.product.model.request.MaterialGroupChangeParentRequest;
import com.ssy.lingxi.product.model.request.MaterialGroupRequest;
import com.ssy.lingxi.product.model.resp.MaterialGroupDetailResponse;
import com.ssy.lingxi.product.service.material.IMaterialGroupService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @menu 商品能力-物料组管理
 * @author hsl
 * @version 2.0.0
 * @date 22/03/22 16:50
 */
@RestController
@RequestMapping("/product/materialGroup")
public class MaterialGroupController extends BaseController {

    @Resource
    private IMaterialGroupService materialGroupService;

    /**
     * 获取物料组树
     * @param rootNodeId 根节点id
     * @param excludeId 排除id（用于选择移动物料组）
     * @return 树形列表
     */
    @GetMapping("/tree")
    public Wrapper<List<Node>> tree(@RequestParam(value = "rootNodeId", required = false) Long rootNodeId,@RequestParam(value = "excludeId",required = false) Long excludeId){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return materialGroupService.getMaterialGroupTree(sysUser,rootNodeId,excludeId);

    }

    /**
     * 获取物料组详情
     * @param commonIdVO idVo
     * @return 物料组详情
     */
    @GetMapping("/detail")
    public Wrapper<MaterialGroupDetailResponse> detail(@Valid CommonIdVO commonIdVO){
        UserLoginCacheDTO sysUser = this.getSysUser();

        MaterialGroup materialGroup= materialGroupService.getMaterialGroup(commonIdVO.getId());
        if (materialGroup != null){
            MaterialGroupDetailResponse materialGroupDetailResponse = this.modelMapper.map(materialGroup, MaterialGroupDetailResponse.class);

            return Wrapper.success(materialGroupDetailResponse);
        }
        return Wrapper.success(null);

    }

    /**
     * 新增修改物料
     * @param materialGroupRequest 物料组请求
     * @return 默认成功信息
     */
    @PostMapping("/saveOrUpdate")
    public Wrapper<Void> add(@RequestBody @Valid MaterialGroupRequest materialGroupRequest ){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return materialGroupService.saveOrUpdateMaterialGroup(sysUser, this.modelMapper.map(materialGroupRequest, MaterialGroup.class));
    }

    /**
     * 移动物料组
     * @param materialGroupChangeParentRequst 移动物料组请求
     * @return 默认成功信息
     */
    @PostMapping("/changeParent")
    public Wrapper<Void> changeParent(@RequestBody @Valid MaterialGroupChangeParentRequest materialGroupChangeParentRequst){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return materialGroupService.changeParent(sysUser,materialGroupChangeParentRequst);
    }

    /**
     * 删除物料组
     * @param commonIdVO idVo
     * @return 默认成功信息
     */
    @PostMapping("/delete")
    public Wrapper<Void> delete(@RequestBody @Valid CommonIdVO commonIdVO){

        UserLoginCacheDTO sysUser = this.getSysUser();

        return materialGroupService.delete(sysUser,commonIdVO.getId());
    }




}
